exports.homeData = (req, res) => {

    var restfulParams = req.routeInfo.restfulParams;

    var resultData = {};

    var todayStart = new Date(new Date().toLocaleDateString()).getTime() / 1000;
    var todayEnd = (new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000) / 1000;
    var is_manager = restfulParams.is_manager;
    var u_type = restfulParams.u_type;
    var u_osid = restfulParams.u_osid;
    var c_uid_condition = '';
    var apo_invite_code_condition = '';
    var cfr_uid_condition = '';
    var sq_reply_uid_condition = '';
    var newCustomerTodayCount = 0;//今日新增
    var followUpTodayCount = 0;//今日跟进
    var dealTodayCount = 0;//今日成交
    var totalNumberCount = 0;//总人数
    var catchingUpCount = 0;//跟进中
    var dealConcludedCount = 0;//已成交
    var waitingDealFeedbackCount = 0;//待处理反馈
    var waitingForServiceCount = 0;//待服务
    var waitForFlowingCount = 0;//待跟进
    var salesArr = [];
    var setACount = 0;
    var setBCount = 0;
    var setCCount = 0;
    var userInStr = "";
    var flowingCustomers = 0;

    function salesSearch(cb) {
        if (is_manager == 1 && (u_type == 1 || u_type == 2)) {

            //战略  查询战略伙伴下所有销售
            console.log("战略 || 代理");
            R.SEND_HTTP(
                req,
                res, {
                    url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_master_osid/" + u_osid,
                    method: 'POST',
                    data: {
                        fields: ['u_id'].join(",")
                    }
                },
                function (data) {
                    console.log("11111111")
                    console.log(data);

                    salesArr = data.result.data;

                    cb();

                }
            )
        } else if (is_manager == 1 && u_type == 3) {
            //销售主管  查询该销售主管下所有销售.包括自己。
            console.log("销售主管");
            R.SEND_HTTP(
                req,
                res, {
                    url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_leader/" + restfulParams.u_id,
                    method: 'POST',
                    data: {
                        fields: ['u_id'].join(",")
                    }
                },
                function (data) {
                    console.log("222222222")
                    console.log(data);

                    salesArr = data.result.data;

                    cb();

                }
            )
        } else if (is_manager == 0 && u_type == 3) {
            //普通销售
            console.log("普通销售");
            salesArr.push({
                u_id: restfulParams.u_id
            });
            cb();
        } else if (u_type == 0) {
            //超管
            console.log("超管");
            R.SEND_HTTP(
                req,
                res, {
                    url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url,
                    method: 'POST',
                    data: {
                        fields: ['u_id'].join(",")
                    }
                },
                function (data) {

                    console.log("333333333")
                    console.log(data);

                    salesArr = data.result.data;

                    cb();

                }
            )
        }

    }

    // 拼接in语句查询条件
    function fixInCondition(cb) {

        console.log(444444444)

        for (var i = 0; i < salesArr.length; i++) {
            userInStr += salesArr[i].u_id + ","
        }
        userInStr = userInStr.substring(0, userInStr.length - 1);

        if (userInStr == '') {
            userInStr = "null";
        }

        if (u_type > 0) {
            c_uid_condition = "/in/c_uid-" + userInStr;
            apo_invite_code_condition = "/in/apo_invite_code-" + userInStr;
            cfr_uid_condition = "/in/cfr_uid-" + userInStr;
            sq_reply_uid_condition = "/in/sq_reply_uid-" + userInStr;
        }

        cb()
    }

    function queryForServiceOrder(cb) {
        console.log(5555555)
        //查询每个待服务订单
        R.SEND_HTTP(
            req,
            res, {
                url: config.external_server.apiAddress + "/organization/search_art_product_order/count/1/apo_status/1/apo_service_status/0" + apo_invite_code_condition,
                method: 'POST',
                data: {
                    fields: ['apo_id'].join(",")
                }
            },
            function (data) {
                waitingForServiceCount = parseInt(data.result.count);
                cb();

            }
        )
    }

    //查询待跟进客户
    function searchWaitForFlowing(cb) {
        console.log(666666)
        R.SEND_HTTP(
            req,
            res, {
                url: CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/count/1/c_follow_status/0/c_is_delete/0" + c_uid_condition,
                method: 'POST',
                data: {
                    fields: ['c_id'].join(",")
                }
            },
            function (data) {

                waitForFlowingCount = parseInt(data.result.count);

                cb();

            }
        )
    }

    //今日新增
    function newCustomerToday(cb) {
        console.log(7777777)
        R.SEND_HTTP(
            req,
            res, {
                url: CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/count/1/c_is_delete/0" + c_uid_condition + "/between/" + todayStart + "-c_create_time-" + todayEnd,
                method: 'POST',
                data: {
                    fields: ['c_id'].join(",")
                }
            },
            function (data) {

                newCustomerTodayCount = parseInt(data.result.count);

                cb();

            })

    }

    //今日跟进
    function followUpToday(cb) {

        R.SEND_HTTP(
            req,
            res, {
                url: CORE_APIConfig.coreAPI.artCustomerFollowRecordSearch.Request_Api_Url + "/count/1/cfr_is_delete/0" + cfr_uid_condition + "/between/" + todayStart + "-cfr_create_time-" + todayEnd,
                method: 'POST',
                data: {
                    fields: ['cfr_id'].join(",")
                }
            },
            function (data) {

                followUpTodayCount = parseInt(data.result.count);

                cb();

            }
        )
    }

    //今日成交
    function dealToday(cb) {
        console.log(8888888)
        F.ParallelEach(salesArr, function (item, index, callback) {

            if (u_type > 0) {
                apo_invite_code_condition = "/apo_invite_code/" + item.u_id;
            }
            R.SEND_HTTP(
                req,
                res, {
                    url: config.external_server.apiAddress + "/organization/search_art_product_order/count/1/apo_status/1" + apo_invite_code_condition + "/between/" + todayStart + "-apo_create_time-" + todayEnd,
                    method: 'POST',
                    data: {
                        fields: ['apo_id'].join(",")
                    }
                },
                function (data) {

                    dealTodayCount += parseInt(data.result.count);

                    callback();

                }
            )
        }, cb)
    }

    //跟进中
    function catchingUp(cb) {
        console.log(9999999)
        R.SEND_HTTP(
            req,
            res, {
                url: CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/count/1/c_follow_status/1/c_is_delete/0" + c_uid_condition,
                method: 'POST',
                data: {
                    fields: ['c_id'].join(",")
                }
            },
            function (data) {

                catchingUpCount = parseInt(data.result.count);

                cb();

            }
        )
    }

    //已成交
    function dealConcluded(cb) {
        console.log(101010101)
        // console.log(CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/count/1/" + c_uid_condition + "/c_is_buy/1/sort_order/c_create_time-desc");
        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/count/1/" + c_uid_condition + "/c_is_buy/1/sort_order/c_create_time-desc",
                method: 'POST',
                data: {
                    fields: ['c_id'].join(",")
                }
            },
            function (data) {

                dealConcludedCount = parseInt(data.result.count);
                cb();

            }
        )
    }

    //查询待处理异议信息
    function waitingDealFeedbackInfo(cb) {
        console.log(1111111111)
        R.SEND_HTTP(
            req,
            res, {
                url: CORE_APIConfig.coreAPI.artSalesQuestionSearch.Request_Api_Url + "/count/1/sq_status/0/sq_is_delete/0" + sq_reply_uid_condition,
                method: 'POST',
                data: {
                    fields: ['sq_id'].join(",")
                }
            },
            function (data) {

                waitingDealFeedbackCount = parseInt(data.result.count);

                cb();

            }
        )
    }

    function totalNumber(cb) {

        R.SEND_HTTP(
            req,
            res, {
                url: CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/count/1/c_is_delete/0" + c_uid_condition,
                method: 'POST',
                data: {
                    fields: ['c_id', 'c_openid'].join(",")
                }
            },
            function (data) {

                totalNumberCount = parseInt(data.result.count);

                cb();

            }
        )
    }

    //a类客户总人数
    function setA(cb) {
        console.log(2222222)
        R.SEND_HTTP(
            req,
            res, {
                url: CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/count/1/c_is_delete/0" + c_uid_condition + "/c_evaluation/0",
                method: 'POST',
                data: {
                    fields: ['c_id', 'c_openid'].join(",")
                }
            },
            function (data) {

                setACount = parseInt(data.result.count);

                cb();

            }
        )
    }

    //b类客户总人数
    function setB(cb) {
        console.log("aaaaaaaaa")
        R.SEND_HTTP(
            req,
            res, {
                url: CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/count/1/c_is_delete/0" + c_uid_condition + "/or/c_evaluation-1,2",
                method: 'POST',
                data: {
                    fields: ['c_id', 'c_openid'].join(",")
                }
            },
            function (data) {

                setBCount = parseInt(data.result.count);

                cb();

            }
        )
    }

    //c类客户总人数
    function setC(cb) {

        R.SEND_HTTP(
            req,
            res, {
                url: CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/count/1/c_is_delete/0" + c_uid_condition + "/or/c_evaluation-3,4",
                method: 'POST',
                data: {
                    fields: ['c_id', 'c_openid'].join(",")
                }
            },
            function (data) {

                setCCount = parseInt(data.result.count);

                cb();

            }
        )
    }

    //查询跟进中客户名称，电话，省市区+详细地址，跟进状态，客户评定
    function searchForFlowingCustomer(cb) {
        console.log("bbbbbbbb")
        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/c_follow_status/1/c_is_delete/0" + c_uid_condition,
                method: 'POST',
                data: {
                    fields: ['c_id', 'c_openid'].join(",")
                }
            },
            function (data) {

                flowingCustomers = data.result.data;

                cb();

            }
        )
    }

    //查询每个跟进中客户的最后一条跟进记录
    function searchLastFollowUpRecordOfEachCustomers(cb) {
        console.log("cccccccc")
        // ParallelEach SerialEach
        // F.ParallelEach(flowingCustomers, function (item, index, callback) {

        //     //获取该代理下所有销售的订单数据
        //     R.SEND_HTTP(
        //         req,
        //         res,
        //         {
        //             url: CORE_APIConfig.coreAPI.artCustomerFollowRecordSearch.Request_Api_Url + "/cfr_is_delete/0/cfr_cid/" + item.c_id + "/sort_order/cfr_id-desc/skip/0/limit/1",
        //             method: 'POST',
        //             data: {
        //                 fields: ['cfr_cid', 'cfr_next_time'].join(',')
        //             }
        //         },
        //         function (data) {
        //             var isNextFollow = data.result.data;
        //             var nowTime = common.nowTime(false);
        //             if (isNextFollow.length > 0) {
        //                 if(isNextFollow[0].cfr_next_time != 0){
        //                     if (parseInt(isNextFollow[0].cfr_next_time) < nowTime) {
        //                         waitForFlowingCount++
        //                     }
        //                     callback()
        //                 }else{
        //                     callback();
        //                 }
        //             }else{
        //                 callback();
        //             }
        //         }
        //     )
        // }, cb)
        cb();
    }

    //各项数据统计
    function statisticalData(cb) {
        resultData.newCustomerTodayCount = newCustomerTodayCount;
        resultData.followUpTodayCount = followUpTodayCount;
        resultData.dealTodayCount = dealTodayCount;
        resultData.totalNumberCount = totalNumberCount;
        resultData.catchingUpCount = catchingUpCount;
        resultData.dealConcludedCount = dealConcludedCount;
        resultData.waitingDealFeedbackCount = waitingDealFeedbackCount;
        resultData.waitForFlowingCount = waitForFlowingCount;
        resultData.setACount = setACount;
        resultData.setBCount = setBCount;
        resultData.setCCount = setCCount;
        cb()
    }

    F.Serial([
        salesSearch,
        fixInCondition,
        queryForServiceOrder,
        searchWaitForFlowing,
        newCustomerToday,
        followUpToday,
        dealToday,
        catchingUp,
        dealConcluded,
        waitingDealFeedbackInfo,
        totalNumber,
        setA,
        setB,
        setC,
        searchForFlowingCustomer,
        searchLastFollowUpRecordOfEachCustomers,
        statisticalData,
    ], () => {
        RES.successResponse(res, resultData);
    })

}